package com.chat.view;

import com.chat.Local;
import com.chat.common.User;
import com.chat.service.CheckEmail;
import com.chat.service.CheckSecurityCode;
import com.chat.service.EmailService;
import com.chat.service.UserClientService;
import com.chat.utils.IsMail;
import com.chat.utils.MouseFollow;
import com.chat.utils.SecurityCode2;
import com.chat.utils.SecurityCode3;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.apache.commons.lang.RandomStringUtils;

import java.io.IOException;

public class EmailLoginController {
    public Label prompt;
    public TextField email;
    public TextField code;
    public Button sendbutton;
    public String content;
    public void initialize(){
        prompt.setText("点击发送验证码");
    }

    public void send(MouseEvent mouseEvent) {
        if(IsMail.isEmail(email.getText().toString())&& CheckEmail.checkemail(email.getText().toString())==false)
        {   sendbutton.setDisable(true);
            SecurityCode3 securityCode2=new SecurityCode3(this);
            securityCode2.start();
            String s= RandomStringUtils.randomNumeric(6);
            content=RandomStringUtils.randomAlphabetic(6);
            String address=email.getText().toString();
            boolean b = EmailService.emailservice(content,address);
            System.out.println("发送"+(b?"成功":"失败"));
        }
        else{
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
         alert.setTitle("提示");
         alert.setHeaderText("警告");
         alert.setContentText("邮箱无效！");
         alert.showAndWait();
        }
    }

    public void test1(int time){
        Platform.runLater(()-> {
            prompt.setText("重新发送还需等待"+"("+time+")"+"秒");
        });
    }

    public void test2() {
        Platform.runLater(()-> {
            prompt.setText("点击重新发送验证码");
            sendbutton.setDisable(false);        });

    }

    public void login() throws IOException, ClassNotFoundException {
        CheckSecurityCode csc=new CheckSecurityCode();
        if(csc.check(code.getText(),email.getText())) {//csc.check(code.getText(),email.getText())
            int rs=UserClientService.getUser(email.getText());
            if(rs==1) {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("提示");
                alert.setHeaderText("恭喜");
                alert.setContentText("登录成功！");
                alert.showAndWait();
                createmainwindow();
            }
        }
        else {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示");
            alert.setHeaderText("警告");
            alert.setContentText("验证码错误！");
            alert.showAndWait();
        }
    }
    public void createmainwindow(){
        try {
                    Stage stage=(Stage) sendbutton.getScene().getWindow();
                stage.close();
            MainWindowView.start(new Stage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void return1(){
        Stage stage=(Stage) sendbutton.getScene().getWindow();
        stage.close();
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("login.fxml"));
            Parent root = fxmlLoader.load();
            Stage regitsterstage=new Stage();
            Scene scene = new Scene(root, 600, 400);
            regitsterstage.initStyle(StageStyle.TRANSPARENT);
            MouseFollow in = new MouseFollow(root, regitsterstage);
            regitsterstage.setScene(scene);
            regitsterstage.show();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
